package com.yiren.dbaa.module.practice_set.map.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.constant.DbConstant;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.TrajectDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.currency.event.FragmentRefreshEvent;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.practice_set.line.view.activity.AddLineActivity;
import com.yiren.dbaa.module.practice_set.map.contract.MapHomeContract;
import com.yiren.dbaa.module.practice_set.map.presenter.MapHomePresenter;
import com.yiren.dbaa.module.practice_set.map.view.activity.AddMapActivity;
import com.yiren.dbaa.module.practice_set.map.view.activity.MapCollectActivity;
import com.yiren.dbaa.module.practice_set.map.view.activity.MapManageActivity;
import com.yiren.dbaa.module.practice_set.map.view.adapter.MapHomeListAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/11/24
 */
public class MapHomeFragment extends LazyLoadFragment implements MapHomeContract.View {

    @BindView(R.id.lv_map_data)
    ListView lvMapData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.ll_add_map)
    LinearLayout llADddMap;

    private MapHomeContract.Presenter mapHomePresenter;
    private UserParamConfigBean userParamConfig;
    private boolean isPartner;//是否是合伙人视角
    private List<MapEntity> mapListSer;

    @Override
    protected int setContentView() {
        return R.layout.fragment_map_home;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        userParamConfig = LoadConfigApi.loadUserParamConfig(getActivity());
        isPartner = userParamConfig.isPartner();
        mapHomePresenter = new MapHomePresenter(getActivity());
        llADddMap.setVisibility(isPartner ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapHomePresenter.loadMapList(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(FragmentRefreshEvent event) {
        mapHomePresenter.loadMapList(this);
    }

    @OnClick({R.id.ll_add_map})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_add_map:
                Intent intent = new Intent(getActivity(), AddMapActivity.class);
                intent.putExtra("mapListSer", (Serializable) mapListSer);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void showMap(List<MapEntity> mapList) {
        mapListSer = mapList;
        if (mapList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvMapData.setVisibility(View.GONE);
            tvTips.setText("您当前考规下地图列表为空，请先新增地图!");
        } else {
            llNoData.setVisibility(View.GONE);
            lvMapData.setVisibility(View.VISIBLE);
            MapHomeListAdapter mapHomeListAdapter = new MapHomeListAdapter(getActivity(), mapList);
            lvMapData.setAdapter(mapHomeListAdapter);

            mapHomeListAdapter.setOnManageClickListener(position -> {
                if (mapList.get(position).isDownLoad()) {
                    if (isPartner) {
                        Intent intent = new Intent(getActivity(), MapManageActivity.class);
                        intent.putExtra("map", mapList.get(position));
                        startActivity(intent);
                    } else {
                        DbConstant.TRAJECT_DATA_URL = DbConstant.App_Main_Directory + userParamConfig.getUserInfo().getId() + "/map/" + mapList.get(position).getSiteCode() + "/" + DbConstant.TRAJECT_DATA_NAME;
                        TrajectDbController.getInstance(true);
                        startActivity(new Intent(getActivity(), MapCollectActivity.class));
                    }
                } else {
                    mapHomePresenter.loadSiteMap(mapList.get(position), MapHomeFragment.this);
                }
            });

            mapHomeListAdapter.setOnDeleteClickListener(position -> {
                CommonDialog.showTwoBtn(getActivity(), "操作确认", "确定删除" + mapList.get(position).getName() + "吗？", "取消", "确认", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        mapHomePresenter.deleteMap(mapList.get(position), new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                mapList.remove(position);
                                mapHomeListAdapter.notifyDataSetChanged();
                                dialog.dismiss();
                            }
                        });
                    }
                });
            });
        }
    }

    @Override
    public void refreshMapList() {
        mapHomePresenter.loadMapList(this);
    }
}
